package com.meshnetics.gb.type.impl.zcl;

/**
 * WRITE DESCRIPTION HERE
 *
 * @author AKovylin
 */
public class UInt extends FixedBytesDataValue {
    // write comment - constructor only for extension, no checks or cloning performed!
    // (to avoid double checks/cloning, checks/cloning must be done by type on deserialization)
    protected UInt(TypeUInt type, byte[] value) {
        super(type, value);
    }

    public boolean isInvalid() {
        boolean res = true;
        for (int i = 0; i < value.length; i++) {
            if (value[i] != (byte) 0xff) {
                res = false;
                break;
            }
        }
        return res;
    }

    protected boolean isUnsigned() {
        return true;
    }

    public UInt convert(TypeUInt type) {
        byte[] newValue = new byte[type.getByteSize()];
        if (newValue.length <= value.length) {
            System.arraycopy(value, 0, newValue, 0, newValue.length);
        } else {
            System.arraycopy(value, 0, newValue, 0, value.length);
            for (int i = value.length; i < newValue.length; i++) { // unsigned - always extending with 0x00
                newValue[i] = 0x00;
            }
        }
        return (UInt) type.createValue(newValue);
    }
}
